package com.example.file_management.service;

import com.example.file_management.mapper.material.ProjectMapper;
import com.example.file_management.pojo.material.Project;
import com.example.file_management.service.RoleService;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@JsonSerialize(using = LocalDateTimeSerializer.class)
@JsonDeserialize(using = LocalDateTimeDeserializer.class)
public class ProjectService {

    @Autowired
    private ProjectMapper projectMapper;

    @Autowired
    private RoleService roleService;

    // 插入新项目
    public int createProject(Project project, String username) {
        if (!roleService.hasPermission(username)) {
            throw new SecurityException("没有足够的权限创建项目");
        }
        return projectMapper.insertProject(project);
    }

    // 根据ID获取项目
    public Project getProjectById(Long id) {
        return projectMapper.getProjectById(id);
    }

    // 获取所有项目
    public List<Project> getAllProjects() {
        return projectMapper.getAllProjects();
    }

    // 更新项目信息
    public int updateProject(Project project, String username) {
        if (!roleService.hasPermission(username)) {
            throw new SecurityException("没有足够的权限更新项目");
        }
        return projectMapper.updateProject(project);
    }

    // 删除项目
    public int deleteProject(Long id, String username) {
        if (!roleService.hasPermission(username)) {
            throw new SecurityException("没有足够的权限删除项目");
        }
        return projectMapper.deleteProject(id);
    }
}